package com.fr.data.core.define;

import com.fr.base.FRContext;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.util.ListSet;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.ChartData;
import com.fr.data.ChartDataModel;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.data.core.DataXMLUtils;
import com.fr.data.impl.ReportChartData;
import com.fr.data.util.function.DataFunction;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class OneValueCDDefinition extends TableDataDefinition
{
  private static final long serialVersionUID = 2093363235824337628L;
  public static final String XML_TAG = "OneValueCDDefinition";
  private String seriesColumnName;
  private String valueColumnName;
  private DataFunction dataFunction = null;

  public OneValueCDDefinition()
  {
  }

  public OneValueCDDefinition(TableData paramTableData, String paramString1, String paramString2, String paramString3, DataFunction paramDataFunction)
  {
    super(paramTableData, paramString1);
    setSeriesColumnName(paramString2);
    setValueColumnName(paramString3);
    setDataFunction(paramDataFunction);
  }

  public void reset()
  {
    super.reset();
    this.seriesColumnName = null;
    setValueColumnName(null);
    this.dataFunction = null;
  }

  protected int createSeriesNameArray(List paramList)
    throws TableDataException
  {
    int i = -1;
    int j = this.tableData.getColumnCount();
    for (int k = 0; k < j; ++k)
    {
      ListSet localListSet;
      int i1;
      if (ComparatorUtils.tableDataColumnNameEquals(this.tableData.getColumnName(k), getSeriesColumnName()))
      {
        localListSet = new ListSet();
        i = k;
        int l = this.tableData.getRowCount();
        for (i1 = 0; i1 < l; ++i1)
        {
          Object localObject = this.tableData.getValueAt(i1, k);
          localListSet.add(localObject);
        }
        for (i1 = 0; i1 < localListSet.size(); ++i1)
          paramList.add(localListSet.get(i1));
      }
    }
    return i;
  }

  public ChartData createChartData()
  {
    if (this.tableData == null)
      return ChartDataModel.defaultChartData;
    ReportChartData localReportChartData = ChartDataModel.defaultChartData;
    ArrayList localArrayList1 = new ArrayList();
    ArrayList localArrayList2 = null;
    try
    {
      int i4;
      ArrayList localArrayList3 = new ArrayList();
      int i = createCategoryNameArray(localArrayList3);
      Object[] arrayOfObject1 = localArrayList3.toArray();
      ArrayList localArrayList4 = new ArrayList();
      int j = createSeriesNameArray(localArrayList4);
      Object[] arrayOfObject2 = localArrayList4.toArray();
      int k = this.tableData.getColumnCount();
      int l = -1;
      for (int i1 = 0; i1 < k; ++i1)
        if (ComparatorUtils.tableDataColumnNameEquals(this.tableData.getColumnName(i1), getValueColumnName()))
          l = i1;
      i1 = this.tableData.getRowCount();
      DataFunction localDataFunction = getDataFunction();
      if (i == -1)
      {
        if ((localArrayList3 == null) || (localArrayList3.toArray().length != 1))
          break label463;
        i2 = 0;
        i3 = arrayOfObject2.length;
        while (true)
        {
          if (i2 >= i3)
            break label463;
          localArrayList2 = new ArrayList();
          for (i4 = 0; i4 < i1; ++i4)
            if (this.tableData.getValueAt(i4, j).equals(arrayOfObject2[i2]))
              localDataFunction.addData(this.tableData.getValueAt(i4, l));
          localArrayList2.add(localDataFunction.getResult());
          localDataFunction.reset();
          localArrayList1.add(localArrayList2.toArray());
          ++i2;
        }
      }
      int i2 = 0;
      int i3 = arrayOfObject2.length;
      while (i2 < i3)
      {
        localArrayList2 = new ArrayList();
        i4 = 0;
        int i5 = arrayOfObject1.length;
        while (i4 < i5)
        {
          for (int i6 = 0; i6 < i1; ++i6)
            if ((this.tableData.getValueAt(i6, i).equals(arrayOfObject1[i4])) && (this.tableData.getValueAt(i6, j).equals(arrayOfObject2[i2])))
              localDataFunction.addData(this.tableData.getValueAt(i6, l));
          localArrayList2.add(localDataFunction.getResult());
          localDataFunction.reset();
          ++i4;
        }
        localArrayList1.add(localArrayList2.toArray());
        ++i2;
      }
      label463: Object[][] arrayOfObject = (Object[][])localArrayList1.toArray(new Object[localArrayList1.size()][]);
      localReportChartData = new ReportChartData(arrayOfObject1, arrayOfObject2, arrayOfObject);
    }
    catch (TableDataException localTableDataException)
    {
      FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
    }
    try
    {
      this.tableData.release();
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return localReportChartData;
  }

  public void setSeriesColumnName(String paramString)
  {
    this.seriesColumnName = paramString;
  }

  public String getSeriesColumnName()
  {
    return this.seriesColumnName;
  }

  public void setValueColumnName(String paramString)
  {
    this.valueColumnName = paramString;
  }

  public String getValueColumnName()
  {
    return this.valueColumnName;
  }

  public void setDataFunction(DataFunction paramDataFunction)
  {
    this.dataFunction = paramDataFunction;
  }

  public DataFunction getDataFunction()
  {
    return this.dataFunction;
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("OneValueCDDefinition");
    if (this.seriesColumnName != null)
      paramXMLPrintWriter.attr("seriesName", this.seriesColumnName);
    if (getValueColumnName() != null)
      paramXMLPrintWriter.attr("valueName", getValueColumnName());
    if (getDataFunction() != null)
      paramXMLPrintWriter.attr("function", getDataFunction().getClass().getName());
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    String str1;
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isAttr())
    {
      if ((str1 = paramXMLableReader.getAttr("seriesName")) != null)
        this.seriesColumnName = str1;
      if ((str1 = paramXMLableReader.getAttr("valueName")) != null)
        setValueColumnName(str1);
      if ((str1 = paramXMLableReader.getAttr("function")) == null)
        return;
      try
      {
        setDataFunction(DataXMLUtils.readXMLDataFunction(str1));
      }
      catch (Exception localException)
      {
        throw new RuntimeException(localException.getMessage(), localException);
      }
    }
    if (paramXMLableReader.isChildNode())
    {
      str1 = paramXMLableReader.getTagName();
      if ("SeriesColumn".equals(str1))
      {
        String str2;
        if ((str2 = paramXMLableReader.getAttr(str1)) != null)
          this.seriesColumnName = str2;
      }
    }
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    OneValueCDDefinition localOneValueCDDefinition = (OneValueCDDefinition)super.clone();
    if (getDataFunction() != null)
      localOneValueCDDefinition.setDataFunction((DataFunction)getDataFunction().clone());
    return localOneValueCDDefinition;
  }
}